<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
use Think\Model;
class UserController extends BaseController {
    function userlist(){
    	$USER = new Model('User');
    	$totalRows = $USER->count();
    	$page = new \Think\Page($totalRows, 10);
    	$userlist = $USER->limit($page->firstRow,$page->listRows)->order('id desc')->select();
    	$this->assign('page',$page->show());
    	$this->assign('userlist',$userlist);
    	$this->display('user-list');
    }

    function delete(){
    	$id = I('id','');
        if($id == '' || $id == 1){
            $this->error('参数错误');
        } else {
            $USER = new Model('User');
            $rs = $USER->where(['id'=>$id])->delete();
            if($rs !== FALSE){
                $this->success('删除成功');
            } else {
                $this->error($USER->getDbError());
            }
        }
    }

    function edit(){
    	if(IS_POST){
    		$id = I('id','');
            if($id != ''){
                $map['id'] = $id;
                if(I('username') != ''){
                    $map['username'] = I('username');
                }
                if(I('mobile') != ''){
                    $map['mobile'] = I('mobile');
                }
                $map['sex'] = I('sex');
                if(I('country') != ''){
                    $map['country'] = I('country');
                }
                if(I('province') != ''){
                    $map['province'] = I('province');
                }
                if(I('city') != ''){
                    $map['city'] = I('city');
                }
                $map['status'] = I('status');
                $map['type'] = I('type');
                if(I('mid') != ''){
                    $map['mid'] = I('mid');
                }
                $USER = D('User');
                // dump($USER->create($map));die;
                if(!$USER->create($map)){
                    $this->error($USER->getError());
                } else {
                    $rs = $USER->save();
                    if($rs !== FALSE){
                        $this->success('编辑成功');
                    } else {
                        $this->error('编辑失败');
                    }
                }
            } else {
                $this->error('参数错误');
            }
    	}else{
    		$id = I('id','');
            if($id != ''){
                $USER = new Model('User');
                $edit = $USER->where(['id'=>$id])->find();
                if($edit){
                    $this->assign('edit',$edit);
                    $this->display('user-edit');
                } else {
                    $this->error('用户不存在');
                }
            }else{
                $this->error('参数错误');
            }
    	}
    }

}
